<script setup>
const visible = ref(false);
const form = reactive({
  name: '',
  department: '',
});
const rules = reactive({
  name: [
    {
      required: true,
      message: '请输入方案名称',
      trigger: 'blur',
    },
  ],
  department: [
    {
      required: true,
      message: '请选择被审计单位',
      trigger: ['blur', 'change'],
    },
  ],
});
const options = ref([
  {
    label: '集团总部',
    value: '集团总部',
  },
  {
    label: '北京子公司',
    value: '北京子公司',
  },
]);
const templateActive = ref('1');
const template = ref([
  {
    key: '1',
    name: '空模板',
    img: new URL('@/assets/images/project-control/template-empty', import.meta.url).href,
  },
  {
    key: '2',
    name: '通用模版',
    img: new URL('@/assets/images/project-control/template-normal', import.meta.url).href,
  },
  {
    key: '3',
    name: '工程结算审计',
    img: new URL('@/assets/images/project-control/template-project', import.meta.url).href,
  },
  {
    key: '4',
    name: '竣工结算审计',
    img: new URL('@/assets/images/project-control/template-final', import.meta.url).href,
  },
  {
    key: '5',
    name: '经济责任审计',
    img: new URL('@/assets/images/project-control/template-economy', import.meta.url).href,
  },
]);
const store = useStore();

const open = () => {
  visible.value = true;
};
const handleSubmit = () => {
  console.log('submit');
  store.commit('CHANGE_PAGE', 'important');
};
const handleBoxClick = (key) => {
  templateActive.value = key;
};

defineExpose({
  open,
});
</script>

<template>
  <el-dialog class="index-home-implementation-plan-create-plan-dialog" title="创建审计实施方案" v-model="visible" :width="696">
    <label class="choose-template">选择模板：</label>
    <div class="template__list">
      <div class="template__item-wrapper" v-for="temp in template" :key="temp.key">
        <div
          class="template__item-box"
          :class="temp.key === templateActive ? 'box-selected' : null"
          @click="handleBoxClick(temp.key)"
          >
          <iconify-icon
            v-if="temp.key === templateActive"
            class="box-selected-icon"
            icon="ep:circle-check-filled"
            :width="13"
            :height="12"
            color="#2E72EF"
          >
          </iconify-icon>
          <picture>
            <source :srcset="`${temp.img}.webp`" type="image/webp">
            <source :srcset="`${temp.img}.png`" type="image/png">
            <img
              :src="`${temp.img}.png`"
              :title="temp.name"
              class="template__item-img"
            >
          </picture>
        </div>
        <label class="template__item-label">{{ temp.name }}</label>
      </div>
    </div>
    <div class="template-form__wrapper">
      <el-form
        :model="form"
        :rules="rules"
        label-width="auto"
        label-position="top"
      >
      <el-form-item label="方案名称：" prop="name">
        <el-input class="department-input" v-model="form.name" placeholder="请输入方案名称" />
      </el-form-item>
      <el-form-item label="选择被审计单位：" prop="department">
        <el-select
          class="department-select"
          v-model="form.department"
          placeholder="请选择被审计单位"
          clearable
        >
          <el-option :label="opt.label" :value="opt.value" v-for="opt in options" :key="opt.value"/>
        </el-select>
      </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">
        创建方案
      </el-button>
    </template>
  </el-dialog>
</template>

<style lang="scss">
.index-home-implementation-plan-create-plan-dialog {
  .el-dialog__body {
    padding-top: 10px;
    padding-bottom: 10px;
  }
}
</style>

<style lang="scss" scoped>
.index-home-implementation-plan-create-plan-dialog {
  .choose-template {
    height: 20px;
    font-size: 14px;
    color: #25292C;
    line-height: 17px;
  }

  .template__list {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    padding: 18px 20px;

    .template__item-wrapper {
      width: 108px;
      display: flex;
      flex-direction: column;

      .template__item-box {
        height: 108px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        background: #EEF2FE;
        border-radius: 2px;
        cursor: pointer;

        .box-selected-icon {
          position: absolute;
          top: 6px;
          right: 6px;
        }

        .template__item-img {
          width: 67px;
          height: 66px;
          object-fit: cover;
        }
      }

      .box-selected {
        border: 2px dotted #2E72EF;
      }

      .template__item-label {
        margin-top: 10px;
        text-align: center;
      }
    }
  }

  .template-form__wrapper {
    margin-left: 20px;

    .department-input {
      width: 604px;
    }

    .department-select {
      width: 604px;
    }
  }
}
</style>
